package com.practice.xml.jaxb;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import com.practice.xml.Company;
import com.practice.xml.CompanyDefinition;
import com.practice.xml.Employee;
import com.practice.xml.EmployeeDefinition;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = CompanyDefinition.NAME)
class JaxbCompany {

	@XmlAttribute(name = CompanyDefinition.ATTRIBUTE_NAME)
	private String name;

	@XmlElementWrapper(name = CompanyDefinition.ELEMENT_EMPLOYEES)
	@XmlElement(name = EmployeeDefinition.NAME)
	private List<JaxbEmployee> employees;
	
	@XmlElementWrapper(name = "workers")
	@XmlElement(name = "worker")
	private List<JaxbWorker> workers;
	
	public List<JaxbWorker> getWorkers() {
		return workers;
	}

	public void setWorkers(List<JaxbWorker> workers) {
		this.workers = workers;
	}

	public JaxbCompany() {
	}

	public JaxbCompany(Company company) {
		this.name = company.getName();
		this.employees = new ArrayList<>();
		for (Employee employee : company.getEmployees()) {
			this.employees.add(new JaxbEmployee(employee));
		}
	}

	public Company toCompany() {
		Company company = new Company();
		company.setName(this.name);
		List<Employee> employees = new ArrayList<>();
		company.setEmployees(employees);
		for (JaxbEmployee employee : this.employees) {
			employees.add(employee.toEmployee());
		}
		return company;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<JaxbEmployee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<JaxbEmployee> employees) {
		this.employees = employees;
	}
}
